require "lib/pdf"

---@param company_name string
---@param company_address string
---@param company_telephone string
---@param company_mail string
---@param invoice_number string
---@param period string
---@param client_name string
---@param client_code string
---@param address string
---@param city string
---@param meter_code string
---@param solar_fee string
---@param solar_kwh string
---@param solar_chf string
---@param day_fee string
---@param day_kwh string
---@param day_chf string
---@param night_fee string
---@param night_kwh string
---@param night_chf string
---@param meter_rent string
---@param vat string
---@param vat_amount string
---@param total_without_vat string
---@param total string
local create_invoice = function(company_name, company_address, company_telephone, company_mail, invoice_number, period,
                                client_name, client_code, address, city, meter_code, solar_fee, solar_kwh, solar_chf,
                                day_fee, day_kwh, day_chf, night_fee, night_kwh, night_chf, meter_rent, vat, vat_amount,
                                total_without_vat, total)
    local pdf = PDF.new()

    local arial = pdf:new_font { name = "Arial" }
    local arial_bold = pdf:new_font { name = "Arial-Bold" }

    local page = pdf:new_page()

    page:begin_text()
    page:set_font(arial, 26)
    page:set_text_pos(70, 730) -- 608 772, margin lat 70
    page:show(company_name)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 14)
    page:set_text_pos(70, 713)
    page:show(company_address)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 690)
    page:show('Tel: ' .. company_telephone)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 675)
    page:show('Email: ' .. company_mail)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 18)
    page:set_text_pos(70, 560)
    page:show("Riepilogo consumi")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 15)
    page:set_text_pos(70, 530)
    page:show("Fornitura energia elettrica")
    page:end_text()

    page:setrgbcolor("stroke", 0, 0, 0)
    page:moveto(70, 500)
    page:lineto(538, 500)
    page:stroke()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 470)
    page:show("Riepilogo consumi numero")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 470)
    page:show(invoice_number)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 440)
    page:show("Periodo")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 440)
    page:show(period)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 420)
    page:show("Nome Cliente")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 420)
    page:show(client_name)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 400)
    page:show("Codice Cliente")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 400)
    page:show(client_code)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 380)
    page:show("Ubicazione")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 380)
    page:show(address)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 360)
    page:show(city)
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(70, 320)
    page:show("Matricola Contatore")
    page:end_text()

    page:begin_text()
    page:set_font(arial, 12)
    page:set_text_pos(250, 320)
    page:show(meter_code)
    page:end_text()

    page:setrgbcolor("stroke", 0, 0, 0)
    page:moveto(70, 280)
    page:lineto(538, 280)
    page:stroke()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(320, 268)
    page:show("cts/kWh")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(410, 268)
    page:show("kWh")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 268)
    page:show("CHF")
    page:end_text()

    page:setrgbcolor("stroke", 0.81, 0.91, 0.92)
    for i = 0, 19 do
        page:moveto(70, 262 - i)
        page:lineto(538, 262 - i)
    end
    page:stroke()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 248)
    page:show("Tariffa energia solare")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(320, 248)
    page:show(solar_fee)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(410, 248)
    page:show(solar_kwh)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 248)
    page:show(solar_chf)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 228)
    page:show("Tariffa diurna acquisto rete e servizi")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(320, 228)
    page:show(day_fee)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(410, 228)
    page:show(day_kwh)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 228)
    page:show(day_chf)
    page:end_text()

    page:setrgbcolor("stroke", 0.81, 0.91, 0.92)
    for i = 0, 19 do
        page:moveto(70, 222 - i)
        page:lineto(538, 222 - i)
    end

    page:stroke()
    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 208)
    page:show("Tariffa notturna acquisto rete e servizi")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(320, 208)
    page:show(night_fee)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(410, 208)
    page:show(night_kwh)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 208)
    page:show(night_chf)
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 188)
    page:show("Canone contatore e servizi")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 188)
    page:show(meter_rent)
    page:end_text()

    page:setrgbcolor("stroke", 0.81, 0.91, 0.92)
    for i = 0, 19 do
        page:moveto(70, 162 - i)
        page:lineto(538, 162 - i)
    end
    page:stroke()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 148)
    page:show("Totale costo elettricita' IVA escluso")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 148)
    page:show(total_without_vat)
    page:end_text()

    --[[ VAT sequence
    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 128)
    page:show(("IVA %s%%"):format(vat))
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 128)
    page:show(vat_amount)
    page:end_text()

    page:setrgbcolor("stroke", 0.81, 0.91, 0.92)
    for i = 0, 19 do
        page:moveto(70, 102 - i)
        page:lineto(538, 102 - i)
    end
    page:stroke()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(70, 88)
    page:show("Importo totale a nostro favore")
    page:end_text()

    page:begin_text()
    page:set_font(arial_bold, 12)
    page:set_text_pos(480, 88)
    page:show(total)
    page:end_text()

    page:setrgbcolor("stroke", 0, 0, 0)
    page:moveto(70, 58)
    page:lineto(538, 58)
    page:stroke()
    --]]

    page:restore()

    page:add()

    pdf:write(("output/%s-%s.pdf"):format(invoice_number, client_name:lower():gsub(" ", "-")))
end

return create_invoice
